function TreeNode(val, left, right) {
    this.val = (val === undefined ? 0 : val)
    this.left = (left === undefined ? null : left)
    this.right = (right === undefined ? null : right)
}
/**
 * @param {TreeNode} root
 * @param {number} x
 * @param {number} y
 * @return {boolean}
 */
/*
堂兄弟节点：--------- 深度相同 父节点不同
获取节点的深度与父节点 并且进行比较 因此 对树进行遍历 当遍历到需要对比的两个节点的时候 停止遍历
判断是否为堂兄弟节点
* */
// 深度优先遍历------DFS
var isCousins = function(root, x, y) {
    // x 的深度与父节点
    let xParent = null,  xDepth = 0, xFound = false;
    // y 的深度与父节点
    let yParent = null, yDepth = 0, yFound = false;

    // 递归的入口
    dfs(root, 0, null)
    // 判断的条件
    return xDepth == yDepth && xParent != yParent

    // 深度优先遍历
    function dfs(root, depth, parent) {
        //边界条件
        if (root == null) return
        //查找
        if (root.val == x) {
            [xParent,xDepth,xFound] = [parent,depth,true]
        }else if (root.val == y) {
            [yParent,yDepth,yFound] = [parent,depth,true]
        }
        //节点都找到 退出遍历
        if (xFound && yFound) {return }
        dfs(root.left, depth + 1, root)
        if (xFound && yFound) {return }
        dfs(root.right, depth + 1, root)
    }
};

var isCousins = function(root, x, y) {
    let depth
    let obj_x = {}
    let obj_y = {}
    dfs(root, null, 0)
    // 判断
    if (obj_x.depth == obj_y.depth && obj_x.parent != obj_y.parent) return true
    return false
    function dfs(root, parent, depth) {
        if (root.val == x) obj_x = {parent, depth}
        else if (root.val == y) obj_y = {parent, depth}

        // 左右子节点存在
        if(root.left) dfs(root.left, root, depth + 1)
        if(root.right) dfs(root.right, root, depth + 1)
    }
}
// 层序遍历
var isCousins = function(root, x, y) {

}
let x = 4;
let y = 3;
let root = {
    val: 1,
    left : {val: 2, left: 4},
    right : {val: 3},
}
console.log(isCousins(root, x, y))